//
//  JSLoginViewController.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/17.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSLoginViewController.h"
#import "JSRegisterViewController.h"
#import "JSResetPwdViewController.h"

@interface JSLoginViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textFieldHeight;
@property (weak, nonatomic) IBOutlet QMUITextField *userNameTF;
@property (weak, nonatomic) IBOutlet QMUITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *resetPwdBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *regiestBtn;
@property (weak, nonatomic) IBOutlet UILabel *thirdDecL;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space1;

@end

@implementation JSLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)didInitialized{
    [super didInitialized];
    self.view.backgroundColor = [UIColor whiteColor];
    self.userNameTF.placeholder = JSLocalizedString(@"loginusernameplacehorld");
    self.pwdTF.placeholder      = JSLocalizedString(@"loginpwdplacehorld");
    [self.resetPwdBtn setTitle:JSLocalizedString(@"loginpresetpwd") forState:UIControlStateNormal];
    [self.loginBtn setTitle:JSLocalizedString(kLocalizableLogin) forState:UIControlStateNormal];
    [self.regiestBtn setTitle:JSLocalizedString(kLocalizableRegister) forState:UIControlStateNormal];
    self.thirdDecL.text = JSLocalizedString(@"loginthirddesc");
    self.textFieldHeight.constant = kRealValue(44);
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.space1.constant = kRealValue(20);
    
    RACSignal *enableSignal = [[self.userNameTF.rac_textSignal combineLatestWith:self.pwdTF.rac_textSignal] map:^id(id value) {
        return @([value[0] length] > 0 && [value[1] length] > 5 && [value[1] length] < 12);
    }];
    self.loginBtn.rac_command = [[RACCommand alloc] initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) {
        
        return [RACSignal empty];
    }];
}
- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated{
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
    self.title = JSLocalizedString(kLocalizableLogin);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)thirdLoginClick:(UIButton *)sender {
}
- (IBAction)resetPwdClick:(UIButton *)sender {
    JSResetPwdViewController *vc = [[JSResetPwdViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)loginClick:(UIButton *)sender {    
    [userManager login:kUserLoginTypePwd params:@{@"username":self.userNameTF.text,@"password":self.pwdTF.text} completion:^(BOOL success, NSString *des) {
        if (des.integerValue == 90001) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"accountOrPwdError") inView:nil animation:YES];
            });
        }
    }];
    
}
- (IBAction)registerClick:(UIButton *)sender {
    JSRegisterViewController *vc = [[JSRegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}



@end
